package dao.imp;

import java.security.interfaces.RSAKey;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;



import pojo.Customer;
import util.DBUtil;
import dao.CustomerDao;

public class CustomerDaoImp implements CustomerDao{

	@Override
	public int addCustomer(Customer customer) {
		int m =AddDao.addCustomer1(customer);
		int id = AddDao.findId(customer.getCname());
		for(String fav:customer.getFavs()){
			m = AddDao.addHobby(id, fav);
			}
		return m;
	}

	@Override
	public int deleteCusTomer(int id) {
		String sql = "delete from customer where id =?";
		int m = 0;
		PreparedStatement pst = null;
		Connection con = DBUtil.createConnection();
		try {
			pst=con.prepareStatement(sql);
			pst.setInt(1, id);
			m = pst.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con,pst,null);
		}
		System.out.println(m);
		return m;
	}

	@Override
	public int deleteCusTomer(String name) {
		String sql = "delete from customer where name =?";
		int m = 0;
		PreparedStatement pst = null;
		Connection con = DBUtil.createConnection();
		try {
			pst=con.prepareStatement(sql);
			pst.setString(1, name);
			m = pst.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con,pst,null);
		}
		System.out.println(m);
		return m;
	}

	@Override
	public int updateCustomer(Customer customer) {
		int m = UpdateDao.updateCustomer(customer);
		m = UpdateDao.updateHobby(customer.getId(), customer.getFavs());
		return m;
	}

	@Override
	public Customer findOneCustomer(String name) {
		Customer customer =null;
		return customer;
	}

	@Override
	public Customer findOneCustomer(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Customer> fiandAllCustomer() {
		// TODO Auto-generated method stub
		List<Customer> list = FindDao.findAll();
		return list;
	}
	
}
